package com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.repository;

import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.model.entity.FxqhAssessReport;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.model.entity.FxqhAssessReportTemplate;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.model.vo.FxqhAssessReportListVo;
import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import reactor.util.function.Tuple2;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Repository
public class FxqhAssessReportRepository {
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public String getLatestTemplateCode() {
        try {
            return jdbcTemplate.queryForObject("select code from fxqh_assess_report where del_flag='0' order by code desc limit 1 offset 0", String.class);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public List<FxqhAssessReport> list(String code, String name) {
        String sql = "select far.*, farr.record_id records  from fxqh_assess_report far left join fxqh_assess_report_record farr on far.id = farr.report_id where far.del_flag ='0' ";
        if (StringUtils.isNotBlank(code)) {
            sql += " and far.code like '%" + code + "'% ";
        }
        if (StringUtils.isNotBlank(name)) {
            sql += " and far.name like '%" + name + "'% ";
        }
        sql += " order by create_time desc";
        List<FxqhAssessReport> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(FxqhAssessReport.class));
        if (query == null) {
            return null;
        }
        List<FxqhAssessReport> list = new ArrayList<>();
        for (FxqhAssessReport fxqhAssessReport : query) {
            List<FxqhAssessReport> collect = list.stream().filter(o -> o.getId().equals(fxqhAssessReport.getId())).collect(Collectors.toList());
            if (Collections.isEmpty(collect)) {
                list.add(fxqhAssessReport);
            } else {
                collect.get(0).getRecords().addAll(fxqhAssessReport.getRecords());
            }
        }
        return list;
    }

    public void delete(String id, String uid, Date updateTime) {
        String sql = "update fxqh_assess_report set del_flag = '1', update_user = '" + uid + "', update_time = '" + updateTime + "' where id = '" + id + "'";
        jdbcTemplate.update(sql);
    }

    public void upload(String templateId, String fileId, String uid, Date updateTime) {
        String sql = "update fxqh_assess_report set file_id = '" + fileId + "', update_user = '" + uid + "', update_time = '" + updateTime + "' where id = '" + templateId + "'";
        jdbcTemplate.update(sql);
    }

    public Integer countByCode(String code) {
        return jdbcTemplate.queryForObject("select count(*) from fxqh_assess_report where del_flag='0' and code='" + code + "'", Integer.class);
    }

    public void insert(FxqhAssessReport fxqhAssessReport) {
        SqlParameterSource source = new BeanPropertySqlParameterSource(fxqhAssessReport);
        String sql = "INSERT INTO fxqh_assess_report(id, code, name, del_flag, create_user, create_time, update_user, update_time, file_id, template_id) values(:id, :code, :name, :delFlag, :createUser, :createTime, :updateUser, :updateTime, :fileId, :templateId)";
        namedParameterJdbcTemplate.update(sql, source);
    }
}
